package frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import frame.data.contrldata;
import function.filewrite;

public class fileopen extends JDialog{
	public fileopen(master ma) {
		setTitle("VW-Edit 打开");
		setBounds(0, 0, 600, 400);
		setLocationRelativeTo(null);
		setIconImage(Toolkit.getDefaultToolkit().getImage("./material/logo/fvw.png"));
		setModal(true);
		setMinimumSize(new Dimension(600, 400));
		
		JFileChooser fc=new JFileChooser();
		fc.setDialogType(JFileChooser.OPEN_DIALOG);
		for(int i=0;i<16;i++)fc.addChoosableFileFilter(getFileFilterOfNum(i));
		fc.setMinimumSize(new Dimension(600, 400));
		int a=fc.showOpenDialog(this);
		if(a==JFileChooser.APPROVE_OPTION) {
			try {
				if(master.fl.openfile(fc.getSelectedFile().getPath().replaceAll("\\\\", "/"))) {
					master.t1l.addtexttoarea(ma);
					master.t2l.updatelinenum(ma);
					contrldata.filetab.updateUI();
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private String filename[][]= {{".txt","文本文档(*.txt)"},{".java","JAVA源文件(*.java)"},{".cpp","C++源文件(*.cpp)"},{".h","C++源文件(*.h)"}
					,{".css","叠层样式表(*.css)"},{".xml","可扩展标记语言文件(*.xml)"},{".py","Python源文件(*.py)"},{".pyw","Python源文件(*.pyw)"}
					,{".bat","批处理文件(*.bat)"},{".cs","C#源码(*.cpp)"},{".json","JSON文件(*.json)"},{".js","JavaScript源文件(*.js)"}
					,{".vw","VW文件(*.vw)"},{".c","C源文件(*.c)"},{".jsp","JavaServer源文件(*.jsp)"},{".lua","Lua源文件(*.lua)"}};
	private FileFilter getFileFilterOfNum(int num) {
		return new FileFilter() {
			@Override
			public String getDescription() {
				return filename[num][1];
			}
			@Override
			public boolean accept(File f) {
				if(f.getName().endsWith(filename[num][0])||f.isDirectory())return true;
				return false;
			}
		};
	}
}
